package com.daykw.wenshu.task;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.daykw.wenshu.config.NutzDaoConfig;
import com.daykw.wenshu.model.SearchDailyUrl;
import com.daykw.wenshu.utils.MD5Util;
import com.daykw.wenshu.utils.SpringContextHolder;

public class DailyDiffJob {

	private static Logger logger = Logger.getLogger(DailyDiffJob.class);

	public void run() {
		try {
			init();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void init() throws ParseException, UnsupportedEncodingException {
		NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);
		String start = "2018-10-30";
		String end = "2018-12-31";
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
		long day = this.getDaySub(start, end);

		Date date = format.parse(start);
		LocalDateTime startDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
		String zg = "最高法院";
		String[] clevel = { "高级法院", "中级法院", "基层法院" };

		List<Record> clist = dao.query("t_wenshu_city", null);

		for (int i = 0; i < day; i++) {

			LocalDateTime ldt = startDateTime.plusDays(i);

			String s = ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

			String url = "http://wenshu.court.gov.cn/list/list/?sorttype=1&conditions=searchWord+++{0}%20TO%20{1}+%E4%B8%8A%E4%BC%A0%E6%97%A5%E6%9C%9F:{0}%20TO%20{1}&conditions=searchWord+"
					+ URLEncoder.encode(zg, "utf-8") + "+++%E6%B3%95%E9%99%A2%E5%B1%82%E7%BA%A7:"
					+ URLEncoder.encode(zg, "utf-8");
			String val = MessageFormat.format(url, s, s);
			this.saveUrl(val, s, null, zg, dao);
			for (String level : clevel) {
				String url1 = "http://wenshu.court.gov.cn/list/list/?sorttype=1&conditions=searchWord+++{0}%20TO%20{1}+%E4%B8%8A%E4%BC%A0%E6%97%A5%E6%9C%9F:{0}%20TO%20{1}&conditions=searchWord+"
						+ URLEncoder.encode(level, "utf-8") + "+++%E6%B3%95%E9%99%A2%E5%B1%82%E7%BA%A7:"
						+ URLEncoder.encode(level, "utf-8");
				for (Record city : clist) {
					String c = city.getString("city");
					String uu = "&conditions=searchWord+" + URLEncoder.encode(c, "utf-8")
							+ "+++%E6%B3%95%E9%99%A2%E5%9C%B0%E5%9F%9F:" + URLEncoder.encode(c, "utf-8");

					String value = url1 + uu;
					value = MessageFormat.format(value, s, s);
					this.saveUrl(value, s, c, level, dao);

				}
			}

		}

	}

	private void saveUrl(String url, String s, String c, String level, NutDao dao) {
		SearchDailyUrl search = new SearchDailyUrl();
		search.setCreate_date(new Date());
		search.setPage(1);
		search.setUrl(url);
		search.setStatus(0);
		search.setStart_date(s);
		search.setEnd_date(s);
		search.setCity(c);
		search.setCourt_level(level);
		search.setSub_status(2);
		String md5 = MD5Util.getMD5(url);
		search.setMd5(md5);
		// search.setKeyword("和龙市人民法院");
		try {
			dao.fastInsert(search);
			logger.info("完成" + s + ",城市:" + c + ",level:" + level + "url成功");
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	public Date localDateTime2Date(LocalDateTime localDateTime) {
		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime zdt = localDateTime.atZone(zoneId);
		Date date = Date.from(zdt.toInstant());
		return date;
	}

	public long getDaySub(String beginDateStr, String endDateStr) {
		long day = 0;
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
		java.util.Date beginDate;
		java.util.Date endDate;
		try {
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
			day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
			// System.out.println("相隔的天数="+day);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return day;
	}

}
